package io.iamting93.email.service;

import io.iamting93.api.EmailApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

/**
 * @author boting.guo
 * @date 2021/11/30 15:47
 */

@DubboService
public class EmailService implements EmailApi {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;

    public boolean sendEmail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        javaMailSender.send(message);
        return true;
    }

    @Override
    public boolean sendEmail(String emailAddress, String content) {
        return sendEmail(emailAddress, "作业验证码", content);
    }
}
